<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class modeluser extends model
{
	
	function __construct(  )
	{
		
		
		
	}
	
	public function newPassword( $email, $password ){
				
		$result=false;
				
		try {
        						
			$db = application::getDb( configuration::db_type );
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);

			$query = sprintf("UPDATE \"%s%s\" SET password='%s' 
								WHERE email='%s';",
								configuration::table_prefix,
	                 			configuration::table_user, 
	                 			$password, 
	                 			$email);

			try {
        		
				$result = $db->setQuery($query);
				$result=true;
				
			}catch (AppException $e) {
				$result=false;
			}
			

	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}
	
	
	public function insertUser( $email, $password ){
				
		$result=false;
				
		try {
        						
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
			$query = sprintf("INSERT INTO \"%s%s\" (email,password) 
										values ('%s','%s');",
						configuration::table_prefix,
	                 	configuration::table_user, 
	                 	$email,
	                 	$password);	
						
								
			//print( $query );
			try {
        		
				$result = $db->setQuery($query);
				$result=true;
				
			}catch (AppException $e) {
				$result=false;
			}
			
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
	
		return $result;
		
	}


	public function existUser( $email ){
				
		$result=false;
		
		try{
					
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
			$query = sprintf("SELECT  count(*) exist 
							FROM \"%s%s\" WHERE email='%s';",
						configuration::table_prefix,
	                 	configuration::table_user, 
	                 	$email);			
	
			$result = $db->getQuery($query);
	
			if($result[0]->exist>0)	
				$result=true;
			else
				$result=false;
		
		}catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}
	
	
	public function login( $email, $password ){
				
		$result=false;
		
		try {
			
			$db = application::getDb( configuration::db_type);
	// 		
			$db->setParameters(configuration::db_server, 
								configuration::db_port, 
								configuration::db_username, 
								configuration::db_password,
								configuration::db_name);
	
			$query = sprintf("SELECT  id, email FROM \"%s%s\" "
							."WHERE email='%s' and password='%s';",
							configuration::table_prefix,
							configuration::table_user, 
							$email, $password);
							
	
			$result = $db->getQuery($query);
	
			// if($result[0]->exist>0)	
				// $result=true;
			// else
				// $result=false;
				
			if( count($result)<1)	
				$result=false;
				
	    }
	    catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}
	
	public function getSchemaTable( $query ){
				
		$result=false;
				
		//$model	=	application::getModel('user');
		try{
			 
			$db = application::getDb( $args->type);
			
			$db->setParameters($args->hostname, $args->port, 
									$args->username, $args->password,
									$args->databasename);
			
			$query=	"select " 
	                	."ic.column_name, "
	                        ."CASE WHEN ic.character_maximum_length IS NULL THEN 0 ELSE ic.character_maximum_length END,"
	                        ."pd.description,"
	                        ."ic.udt_name"
	                       . " "
	                        ."from INFORMATION_SCHEMA.COLUMNS ic"
	                        .""
	                        ." inner join pg_class pc ON pc.relname=ic.table_name"
	                       . ""
	                        ." left join pg_description pd ON pd.objoid = pc.oid"
	                       . ""
	                       . " where"
	                        .""
	                       . " ic.table_name = '"+$query_table+"'"
	                       . " "
	                        ." order by ic.ordinal_position asc";
			
		//$result = $db->getQuery($query);
		}catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}
	
	
	public function getTableFromInQuery( $query ){
				
		$result=false;
		
		try{
					
			$query=	substr($query, 0, strpos("FROM", $query));
		
		}catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;
		
	}

	/*
	public function authentication( $email, $password ){
			
		try{ 
				
			$model	=	application::getModel('user');
			
			$result	=	$model->authentication( $email, $password );
			
			if( $result )
				$result=1;//application::setAttribute('save', '1');
			else
				$result=2;//application::setAttribute('save', '2');
		
		}catch (AppException $e) {
	    	throw new AppException( $e->getMessage() );
	    }
		
		return $result;	
		
	}*/
	

	
}